package com.example.wx.dailyreport.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wx.dailyreport.mapper.DailyreportMapper;
import com.example.wx.dailyreport.pojo.Dailyreport;
import com.example.wx.dailyreport.pojo.Report;
import com.example.wx.dailyreport.service.DailyreportService;
import com.example.wx.dailyreport.service.ReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hulinbo
 * @since 2020-07-28
 */
@Service
@Transactional
public class DailyreportServiceImpl extends ServiceImpl<DailyreportMapper, Dailyreport> implements DailyreportService {

    @Autowired
    private DailyreportMapper dailyreportMapper;

    @Autowired
    private ReportService reportService;

    @Override
    public boolean saveDailyreport(Dailyreport dailyreport, String report) {
        Report saveReport = new Report();
        saveReport.setId(dailyreport.getDailyId());
        saveReport.setReportcontent(report);
        reportService.saveOrUpdate(saveReport);
        dailyreport.setDailyId(saveReport.getId());
        if (dailyreport.getId() == null)
            dailyreport.setCreatetime(LocalDateTime.now());
        if (dailyreport.getStatus() == 1)
            dailyreport.setSubmittime(LocalDateTime.now());
        return saveOrUpdate(dailyreport);
    }

    @Override
    public IPage<Dailyreport> selectDailyreport(Dailyreport dailyreport, String start, String end, String reportcontent, int page, int limit) {
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        if (StringUtils.isEmpty(start))
            startTime = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.ofHours(8));
        else
            startTime = LocalDateTime.parse(start);
        if (StringUtils.isEmpty(end))
            endTime = LocalDateTime.now();
        else
            endTime = LocalDateTime.parse(end);
        int skip = (page - 1) * limit;
        IPage<Dailyreport> iPage = new Page<>(page, limit);
        iPage.setRecords(dailyreportMapper.selectDailyreportBy(dailyreport, startTime, endTime, reportcontent, skip, limit));
        iPage.setTotal(dailyreportMapper.selectDailyreportCount(dailyreport, startTime, endTime, reportcontent));
        return iPage;
    }

    @Override
    public IPage<Dailyreport> selectAdminReport(Dailyreport dailyreport, String start, String end, String reportcontent, int page, int limit) {
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        if (StringUtils.isEmpty(start))
            startTime = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.ofHours(8));
        else
            startTime = LocalDateTime.parse(start);
        if (StringUtils.isEmpty(end))
            endTime = LocalDateTime.now();
        else
            endTime = LocalDateTime.parse(end);
        int skip = (page - 1) * limit;
        IPage<Dailyreport> iPage = new Page<>(page, limit);
        iPage.setRecords(dailyreportMapper.selectAdminReport(dailyreport, startTime, endTime, reportcontent, skip, limit));
        iPage.setTotal(dailyreportMapper.selectAdminReportCount(dailyreport, startTime, endTime, reportcontent));
        return iPage;
    }


}
